//
//  XKWaitViewController.m
//  KKNote
//
//  Created by 徐凯 on 2019/2/23.
//  Copyright © 2019 JoonSheng. All rights reserved.
//

#import "XKWaitViewController.h"
#import "Masonry.h"
#import "PPNetworkHelper.h"

// 屏幕宽高
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define font_14 14.0

#define IPHONE_X    ((SCREEN_WIDTH/SCREEN_HEIGHT) < 0.5)


@interface XKWaitViewController ()
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UIButton *updateBtn;
@property (nonatomic, strong) NSString *cUrl;

@end

@implementation XKWaitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        window.rootViewController = [stryBoard instantiateInitialViewController];
        
    });
    

//    NSString *url = @"http://appid.aigoodies.com/getAppConfig.php?appid=201902019cc2";
//    [PPNetworkHelper GET:url parameters:nil success:^(id responseObject) {
//        NSString *PushKey = responseObject[@"PushKey"];
//
//
//        NSString *ShowWeb = responseObject[@"ShowWeb"];
//
//        NSString *url = responseObject[@"Url"];
//
//        NSString *status = responseObject[@"success"];
//        if([status isEqualToString:@"true"])
//        {
//            if([ShowWeb isEqualToString:@"1"])
//            {
//                _cUrl = url;
//                [self showWebWithUrl:url];
//            }
//            else
//            {
//                [self rr_getAppVersionInfo];
//
//            }
//
//        }
//        else
//        {
//            [self rr_getAppVersionInfo];
//
//        }
//    } failure:^(NSError *error) {
//        NSLog(@"");
//    }];
}


-(void) showWebWithUrl:(NSString *) url
{
    CGFloat top = IPHONE_X ? 44 : 20;
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, top, SCREEN_WIDTH, SCREEN_HEIGHT - top - 30)];
    _webView.backgroundColor = [UIColor redColor];
    //    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    NSURL* urlAddress = [NSURL URLWithString:url];
    NSURLRequest* request = [NSURLRequest requestWithURL:urlAddress];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    CGFloat b = 44;
    if(IPHONE_X)
    {
        b = 44 + 30;
    }
    
//    _webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - b);
    
 
    [self initSetBtn];
}

//前进 后退 刷新 取消
-(void)initSetBtn{
    CGFloat b = 44;
    if(IPHONE_X)
    {
        b = 44 + 30;
    }
    UIView *bageView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - b, SCREEN_WIDTH, b)];
    bageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bageView];
    //顶线
    UIView *topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    topLineView.backgroundColor = [UIColor grayColor];
    [bageView addSubview:topLineView];
    
    //Home
    UIButton *homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.2, 44)];
    [homeBtn setTitle:@"首页" forState:(UIControlStateNormal)];
    [homeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    homeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [homeBtn addTarget:self action:@selector(homeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:homeBtn];
    
    //前进
    UIButton *goBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.4, 0, SCREEN_WIDTH*0.2, 44)];
    [goBtn setImage:[UIImage imageNamed:@"hou"] forState:UIControlStateNormal];
    goBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [goBtn addTarget:self action:@selector(goBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:goBtn];
    //后退
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.2, 0, SCREEN_WIDTH*0.2, 44)];
    [backBtn setImage:[UIImage imageNamed:@"qian"] forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:backBtn];
    //刷新
    _updateBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.6, 0, SCREEN_WIDTH*0.2, 44)];
    [_updateBtn setImage:[UIImage imageNamed:@"xin"] forState:UIControlStateNormal];
    _updateBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [_updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:_updateBtn];
    //取消
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.8, 0, SCREEN_WIDTH*0.2, 44)];
    [cancelBtn setImage:[UIImage imageNamed:@"guan"] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:font_14];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bageView addSubview:cancelBtn];
}


-(void) homeBtnClick
{
    if(_cUrl.length > 0)
    {
        NSURL* urlAddress = [NSURL URLWithString:_cUrl];
        NSURLRequest* request = [NSURLRequest requestWithURL:urlAddress];
        [_webView loadRequest:request];
    }
    
}

//前进
-(void)goBtnClick{
    if (self.webView.canGoForward) {
        [self.webView goForward];
    }
}
//后退
-(void)backBtnClick{
    [self.webView goBack];
}
//刷新
-(void)updateBtnClick{
    [UIView animateWithDuration:0.3 animations:^{
        [_updateBtn setTransform:CGAffineTransformRotate(_updateBtn.transform, M_PI)];
    }];
    [self.webView reload];
}
//取消
-(void)cancelBtnClick{
    if (self.webView.loading) {
        [self.webView stopLoading];
    }
}


- (void)rr_getAppVersionInfo
{
    
}
@end
